package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.dto.MessageInfoDto;
import cn.com.chengmandian.bike.mapper.EbMessageInfoMapper;
import cn.com.chengmandian.bike.model.EbMessageChannel;
import cn.com.chengmandian.bike.model.EbMessageInfo;
import cn.com.chengmandian.bike.model.vo.MessageInfoVo;
import cn.com.chengmandian.bike.model.vo.SysMessageTotalVo;
import cn.com.chengmandian.bike.service.IEbMessageChannelService;
import cn.com.chengmandian.bike.service.IEbMessageInfoService;
import cn.com.chengmandian.service.bike.constants.MessageBusinessTypeEnum;
import cn.com.chengmandian.service.bike.constants.MessageChannelEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 电单车运营-用户消息 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-30
 */
@Service
@Slf4j
public class EbMessageInfoServiceImpl extends ServiceImpl<EbMessageInfoMapper, EbMessageInfo> implements IEbMessageInfoService {

    @Resource
    private EbMessageInfoMapper messageInfoMapper;

    @Autowired
    private IEbMessageChannelService messageChannelService;

    @Override
    public IPage<MessageInfoVo> getPage(MessageInfoDto messageInfoDto) {
        return messageInfoMapper.getPage(Page.of(messageInfoDto.getPageNumber(),messageInfoDto.getPageSize()), messageInfoDto);
    }

    @Override
    public void addSys(Integer memberId, MessageChannelEnum channel, String content, String image, MessageBusinessTypeEnum businessType, Integer businessId) {
        try {
            EbMessageChannel messageChannel = messageChannelService.getSysByName(channel.getName());
            EbMessageInfo messageInfo = new EbMessageInfo();
            messageInfo.setChannelId(messageChannel.getId());
            messageInfo.setMemberId(memberId);
            messageInfo.setContent(content);
            messageInfo.setImage(image);
            messageInfo.setBusinessType(businessType.getCode());
            messageInfo.setBusinessId(businessId);
            this.save(messageInfo);
        } catch (Exception e) {
            log.error("系统消息发送失败", e);
        }
    }

    @Override
    public SysMessageTotalVo total(Integer memberId) {
        return baseMapper.total(memberId);
    }
}
